// 公告相关
var express = require('express')
var router = express.Router()
const { getALL, getOne, exec } = require('../../db')

// 获取所有公告
router.get('/', async (req, res) => {
  let sql = `select * from notice order by notice_time desc`
  const data = await getALL(sql)
  res.send({
    code: '200',
    msg: '查询成功',
    result: data,
  })
})

// 添加公告
router.post('/add', async (req, res) => {
  const { content, title, username } = req.body
  let sql1 = `select id from user where username='${username}'`
  const data1 = await exec(sql1)
  const id = data1[0].id
  let sql2 = `insert into notice (user_id,notice_content,notice_title) values(${id},'${content}','${title}')`
  const data2 = exec(sql2)
  res.send({
    code: 200,
    message: '添加公告成功!',
  })
})
module.exports = router
